<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>计算属性和监视</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
    </head>

    <body>
        <div id="root">
            姓：<input type="text" v-model="firstName" /> <br />
            名：<input type="text" v-model="lastName" />
            <!-- TODO:复杂的插值语法 -->
            <p>{{firstName+'-'+lastName}}</p>

            <p>{{getFullName()}}</p>
            <p>{{getFullName()}}</p>

            <p>{{fullName}}</p>
            <button @click = 'changeFullName'>修改全名</button>
            <!-- 监控 -->
            <p>{{fullName2}}</p>
            <!-- 对象监控 -->
            <p>{{userObj}}</p>
            <input type="text" v-model="userObj.name">

            <p>{{userInfo}}</p>
            <p>{{gender}}</p>

        </div>
        <script>
            Vue.config.productionTip = false;

            let vm = new Vue({
                el: '#root',
                data: {
                    firstName: 'china',
                    lastName: 'vane',
                    fullName2:'',
                    userObj:{
                        name:'zixin',
                        age: 18,

                    },
                    gender:'nan',
                },
                methods:{
                    getFullName(){
                        console.log('getFullName()');
                        return this.firstName + '-' + this.lastName;
                    },
                    changeFullName(){
                        this.fullName = '子-心';
                    },
                },
                computed:{
                    fullName:{
                        get(){
                            console.log('fullName()');
                            return this.firstName + '-' + this.lastName;
                        },
                        set(val) {
                            console.log('setter',val);
                            let arr = val.split('-');
                            this.firstName = arr[0];
                            this.lastName = arr[1];
                        },
                    },
                    userInfo(){
                        let userinfo = 'atguigu';
                        setTimeout(()=>{
                            userinfo ='guigu';
                        },1000);
                        return userinfo;
                    }
                },
                watch:{
                    firstName:{
                        handler(newValue,oldValue) {
                            console.log('watch');
                            this.fullName2 = newValue + '-' + this.lastName;
                            //wathch监控支持异步
                            setTimeout(()=>{
                                this.gender ='nv';
                            },2000);
                        },
                        immediate:true,
                    },
                    userObj: {
                        handler(newValue, oldValue) {
                            console.log('userObj监控');
                        },
                        immediate: true,
                        deep: true,
                    },
                },
            
            });
            vm.getFullName();

            vm.$watch('lastName',function(newValue,oldValue){
                this.fullName2 = this.firstName + '-' + newValue;
            });
        </script>
    </body>
</html>
